var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var usersRouter = require('./routes/users');
var shopRouter = require('./routes/shoplist');
var loveRouter = require('./routes/love')
var banRouter = require('./routes/banans')
const bodyParser = require('body-parser')
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
// app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({extended: false}))

app.use('/users', usersRouter);
// /users/api/login 登录
// /users/api/register 注册                                  
// /shops/api/shoplist
app.use('/shops', shopRouter);

//处理添加藏品的接口
app.use('/loves', loveRouter);
//轮播图的相关接口
app.use('/banans',banRouter)
//  后台删除单个商品  /shops/api/admin/remove
// 错误处理中间件
app.use((err, req, res, next) => {
  // 给用户响应一个信息
  res.json({
      errorCode: 10,
      msg: '请求错误，刷新重试！'
  })
  next()
})

// 404处理中间件
app.use((req, res, next) => {
  // 给用户响应一个信息
  res.json({
      errorCode: 404,
      msg: '路径不合法！'
  })
})

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
